import 'package:flutter/foundation.dart';
import 'package:news/com.comm/core/http/client.dart';
import 'package:news/com.comm/utils/eventbus.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
class User {
  late int id;
  late String nickname;
  late String avatar;
}

class Auth with ChangeNotifier {
  var bus = new EventBus();
  String? _xtoken = "";
  int _referrerId = 1;
  // User _user;
  String get xtoken => _xtoken!;
  int get referrerId => _referrerId;

  Future<void> setXtoken(String token) async {
    _xtoken = token;
    await saveTo();
  }

  setReferrerId(int mReferrerId) async {
    _referrerId = mReferrerId;
    await saveToReferrerId();
  }

  Future load() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String? token = prefs.get("token") as String?;
    if (token != null && token.isNotEmpty) {
      _xtoken = prefs.get("token") as String?;
    }
  }

  loadReferrerId() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    int? referrerId = prefs.getInt("referrerId");
    if (referrerId == null) {
      referrerId = 1;
    }
//    LogUtil.v("referrerId======$referrerId=======");
    _referrerId = referrerId;
  }

  saveToReferrerId() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setInt("referrerId", _referrerId);
  }

  Future<void> saveTo({isLoginAction}) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    if (_xtoken != null && _xtoken!.isNotEmpty) {
      // debugPrint("saveTo-----load=$_xtoken");
      prefs.setString("token", _xtoken!);
      if (isLoginAction != false) {
        // bus.emit(loginAndLogoutEventName, 'login');
      }
    }
  }
  //
  // logout(BuildContext context,{Function logoutSuccessCallback}) async {
  //   this._xtoken = '';
  //   this._referrerId = 1;
  //   this._user = null;
  //   postMemberId="";
  //   var userModel = Provider.of<UserModel>(context, listen: false);
  //   userModel?.loginOut();
  //   // final user = Provider.of<UserModel>(context, listen: false);
  //   // user.productContent=null;
  //   SharedPreferences prefs = await SharedPreferences.getInstance();
  //   prefs.setString("token", '');
  //   bus.emit(loginAndLogoutEventName, 'logout');
  //   clearUsedInvoiceContent();
  //   AnalysisUtils.getinstance().unbindPhoneNumberToYunPush();
  //   // UmengSdk.onProfileSignOff();
  //   if(logoutSuccessCallback != null) {
  //     logoutSuccessCallback();
  //   }
  // }
}
